// NationscapeIdeology
log using NationscapeIdeology.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\Nationscapedataset.dta" // Tausanovitch, Chris and Lynn Vavreck. 2020. Democracy Fund + UCLA Nationscape, October 10-17, 2019 (version 20200814). Retrieved from [URL].  Date accessed: March 09, 2025.

keep late_term_abortion guns_bg wall cap_carbon deportation gender_attitudes_logical gender_attitudes_opportunity path_to_citizenship gender_attitudes_maleboss racial_attitudes_marry racial_attitudes_generations racial_attitudes_tryhard college raise_upper_tax gov_insurance health_subsidies statements_gender_identity group_favorability_blm reparations group_favorability_undocumented ideo5 pid7_legacy interest education weight start_date

// Social justice scale
*Drop missing values
replace statements_gender_identity=. if statements_gender_identity>4 
replace group_favorability_blm=. if group_favorability_blm>4 
replace reparations=. if reparations>2 
replace group_favorability_undocumented=. if group_favorability_undocumented==999 

*Rename so shorter
rename group_favorability_blm blm
rename group_favorability_undocumented undocumented
rename statements_gender_identity genderidentity

*Reverse variables so social justice is coded high
foreach var in blm reparations undocumented {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rblm rreparations rundocumented genderidentity {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srblm srreparations srundocumented sgenderidentity {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srblm srreparations srundocumented sgenderidentity {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale
* Delete missing values
local vars late_term_abortion guns_bg wall cap_carbon deportation path_to_citizenship 

* Loop through each variable and replace values greater than 20 with missing
foreach var in `vars' {
    replace `var' = . if `var' > 20
}

*Rename to conform with Stata character limits
rename gender_attitudes_logical logical
rename gender_attitudes_opportunity opportunity
rename racial_attitudes_generations generations
rename racial_attitudes_marry marry
rename gender_attitudes_maleboss maleboss
rename racial_attitudes_tryhard tryhard
rename path_to_citizenship citizenship 

*Reverse variables so liberalism is coded high
foreach var in late_term_abortion guns_bg cap_carbon citizenship generations logical opportunity {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rlate_term_abortion rguns_bg wall rcap_carbon deportation rlogical ropportunity rcitizenship maleboss marry rgenerations tryhard {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srlate_term_abortion srguns_bg swall srcap_carbon sdeportation srlogical sropportunity srcitizenship smaleboss smarry srgenerations stryhard {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srlate_term_abortion srguns_bg swall srcap_carbon sdeportation srlogical sropportunity srcitizenship smaleboss smarry srgenerations stryhard {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen ALScale = .
replace ALScale = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Create LeftRight Scale
*Delete missing values
replace college=. if college==999 
replace raise_upper_tax=. if raise_upper_tax==999 
replace gov_insurance =. if gov_insurance==999 
replace health_subsidies=. if health_subsidies==999 


*Reverse variables so left is coded high
foreach var in health_subsidies gov_insurance raise_upper_tax college {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rhealth_subsidies rgov_insurance rraise_upper_tax rcollege {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}      


* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in srhealth_subsidies srgov_insurance srraise_upper_tax srcollege {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreLR = 0
gen count_nonzeroLR = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srhealth_subsidies srgov_insurance srraise_upper_tax srcollege {
    replace total_scoreLR = total_scoreLR + `var'
    replace count_nonzeroLR = count_nonzeroLR + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LRScale = .
replace LRScale = total_scoreLR / count_nonzeroLR if count_nonzeroLR > 0

// Ideology and partisan scales
*Delete missing values
replace pid7_legacy=. if pid7_legacy==888
replace ideo5=. if ideo5==999 

*Reverse variables so liberalism/Dem ID is coded high
foreach var in pid7_legacy ideo5 {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

// Correlations
pwcorr SocJusValues rpid7_legacy rideo5 LRScale ALScale [aweight=weight], sig
pwcorr  SocJusValues rpid7_legacy rideo5 LRScale ALScale if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 [aweight=weight], sig

// Ideological Constraint
*Create graduate dummy variable
gen Graduate=.
replace Graduate=0 if inrange(education, 1, 7)
replace Graduate=1 if inrange(education, 8, 11)

pwcorr rblm rreparations rundocumented genderidentity if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 [aweight=weight], sig 
pwcorr rblm rreparations rundocumented genderidentity if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 & interest==1 [aweight=weight], sig 
pwcorr rblm rreparations rundocumented genderidentity if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 & Graduate==1 [aweight=weight], sig 
pwcorr rblm rreparations rundocumented genderidentity if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 & ideo5==1 [aweight=weight], sig 
pwcorr rblm rreparations rundocumented genderidentity if month(dofc(start_date)) >= 10 & month(dofc(start_date)) <= 12 & year(dofc(start_date)) == 2020 & pid7_legacy==1 [aweight=weight], sig 

log close